/*
 * Created by Icodeon Ltd.
 * Date: 05/08/2008
 * 
 * Copyright 2008 All Rights Reserved
 * Icodeon Ltd 
 * St Johns Innovation Centre
 * Cowley Road
 * Cambridge 
 * CB4 0WS United Kingdom
 * 
 * Registered in England and Wales No: 5068195
 * Use is subject to license terms.
 */

using System;
using System.Reflection;
using Icodeon.Util;

namespace Icodeon.Scorm2CC
{
    /// <summary>
    /// Factory method for creating instances of classes implementing the
    /// IPackageConverter interface.
    /// </summary>
    /// <see cref="Icodeon.Scorm2CC.IPackageConverter"/>
    /// <see cref="Icodeon.Scorm2CC.Converter1p1p4"/>
    public class PackageConverterFactory : object
    {
        private static volatile PackageConverterFactory instance;
        private static object syncRoot = new object();
        private Assembly converterAssembly;

        public const string PACKAGE_CONVERTER_IMPL_KEY = "packageConverterImplementation";
        public const string ZIP_UTILS_IMPL_KEY = "zipUtilsImplementation";

        public static PackageConverterFactory Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new PackageConverterFactory();
                        }
                    }
                }
                return instance;
            }
        }

        private PackageConverterFactory()
        {
            this.converterAssembly = this.LoadAssembly();
        }

        private Assembly LoadAssembly()
        {
            return Assembly.GetCallingAssembly();
        }

        public IZipUtils CreateZipUtils(string zipUtilsClassName)
        {
            object inst = this.converterAssembly.CreateInstance(
                    zipUtilsClassName,
                    true,
                    BindingFlags.CreateInstance,
                    null,
                    null,
                    null,
                    null);
            if (inst == null)
            {
                string message = "Factory cannot create class: " +
                    zipUtilsClassName +
                    " for interface IZipUtils";

                throw new NullReferenceException(message);
            }

            return (IZipUtils)inst;
        }

        public IPackageConverter CreatePackageConverter(string converterClassName)
        {
            object inst = this.converterAssembly.CreateInstance(
                    converterClassName,
                    true,
                    BindingFlags.CreateInstance,
                    null,
                    null,
                    null,
                    null);
            if (inst == null)
            {
                string message = "Factory cannot create class: " +
                    converterClassName +
                    " for interface IPackageConverter";

                throw new NullReferenceException(message);
            }

            return (IPackageConverter)inst;
        }
    }
}
